﻿using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Twos.Web.IService;
using Twos.Web.Service.Entities;
using commnn;

namespace Twos.Web.Service
{
   public class JobService: JobIservice
    {
        /// <summary>
        /// 查询Job表的数据
        /// </summary>
        /// <returns></returns>
        public List<JobDTO> GetJobList()
        {
            using (var db = new Model1())
            {
                BaseService<Job> bs = new BaseService<Job>(db);
                var list = bs.GetLists(u => true).Select(m => new JobDTO
                {
                    Id = m.Id,
                    Name = m.Name,
                    Code = m.Code
                }).ToList();

                return list;
            }
        }



        /// <summary>
        /// 分页 模糊查询  
        /// </summary>
        /// <param name="offset">页码</param>
        /// <param name="limit">每页显示的数据量</param>
        /// <param name="rowCount">总数据量</param>
        /// <returns></returns>
        public List<JobDTO> ShowJobList(int offset, int limit, string sort, string order, string name, string code, ref int rowCount)
        {
            using (var db = new Model1())
            {
                BaseService<Job> bs = new BaseService<Job>(db);
                rowCount = 0;
                //Expression<Func<Job, bool>> where = p => p.IsDel == 1;
                //拼接查询表达式【Common封装】
                var query = commnn.PredicateExtensions.True<Job>();
                query = query.And<Job>(u => u.IsDel == 1);

                if (!String.IsNullOrWhiteSpace(name))
                {
                    //where = where.And(r => r.Name.Contains(name));
                    query = query.And<Job>(u => u.Name.Contains(name));
                }

                if (!String.IsNullOrWhiteSpace(code))
                {
                    //where = where.And(r => r.Code == code);
                    query = query.And<Job>(u => u.Code == code);
                }
                var orderBy = commnn.PredicateExtensions.GetSortLambda<Job>(sort);

                bool orderType = order == "asc" ? true : false;
                var list = bs.GetPagedList(offset, limit, ref rowCount, query, orderBy, orderType).Select(m => new JobDTO
                {
                    Id = m.Id,
                    Name = m.Name,
                    Code = m.Code
                }).ToList();
                return list;
            }
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        public bool Add(string name, string code)
        {
            using (var db = new Model1())
            {
                BaseService<Job> jo = new BaseService<Job>(db);
                Job model = new Job()
                {
                    Name = name,
                    Code = code,
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    IsDel = 1
                };

                return jo.Add(model);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Del(string id)
        {
            using (var db = new Model1())
            {
                BaseService<Job> job = new BaseService<Job>(db);
                var mode = db.Job.Find(id);
                mode.IsDel = 0;
                return job.Delete(mode);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool up(string name, string code, string id)
        {
            using (var db = new Model1())
            {
                BaseService<Job> bs = new BaseService<Job>(db);
                var model = db.Job.Find(id);
                model.Name = name;
                model.Code = code;
                return bs.Update(model);
            }
        }

    }
}
